//
//  LookForGoodsViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/12/5.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "LookForGoodsViewModel.h"
#import "LookForGoodsModel.h"
#import "LookForGoodsTableViewCell.h"
#import "GoodsOrStoreNavView.h"
#import "GoodsTopSelectView.h"
#import "AdressAndSortingView.h"
#import "ScreeningView.h"

@interface LookForGoodsViewModel ()<UITableViewDelegate,UITableViewDataSource,GoodsTopDelegate>{
    UIView * selfView;
    UITableView * self_tableView;
    GoodsTopSelectView * topSelectView;
    AdressAndSortingView * screeningView;
    ScreeningView * screenView;
}
@property (nonatomic,strong) NSMutableArray * dataSource;
@end

@implementation LookForGoodsViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        self.dataSource = [NSMutableArray arrayWithCapacity:0];
        selfView = superView;
        tableView.y = tableView.y + kHeightProportion_In_iPhoneTriploid(78);
        tableView.height = tableView.height - kHeightProportion_In_iPhoneTriploid(78);
        tableView.mj_footer.hidden = YES;
        tableView.mj_header.hidden = YES;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(10))];
        tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(10))];
        [superView addSubview:tableView];
        self_tableView = tableView;
        [self createSearchView];
        [self createData];
    }
    return self;
}

-(void)createSearchView{
    GLGWView * searchBankgroundView = [[GLGWView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(44)) inSuperView:selfView];
    searchBankgroundView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    GoodsOrStoreNavView * searchView = [[GoodsOrStoreNavView alloc] initWithFrame:CGRectMake(kWidthProportion_In_iPhoneTriploid(35), kHeightProportion_In_iPhoneTriploid(7), KScreenWidth - kWidthProportion_In_iPhoneTriploid(70), kHeightProportion_In_iPhoneTriploid(30))];
    [searchBankgroundView addSubview:searchView];
    //
    topSelectView = [[GoodsTopSelectView alloc] initWithFrame:CGRectMake(0, kY_With_View(searchBankgroundView, 0), KScreenWidth, kHeightProportion_In_iPhoneTriploid(34)) inSuperView:selfView];
    topSelectView.delegate = self;
}

-(void)createData{
    for (int i=0; i<5; i++) {
        LookForGoodsModel * l_model = [LookForGoodsModel new];
        l_model.isOpen = NO;
        [self.dataSource addObject:l_model];
    }
    [self_tableView reloadData];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    LookForGoodsModel * look_model = self.dataSource[indexPath.row];
    if (look_model.isOpen) {
        return kHeightProportion_In_iPhoneTriploid(181);
    }else{
        return kHeightProportion_In_iPhoneTriploid(136);
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LookForGoodsTableViewCell * cell = [LookForGoodsTableViewCell cellWithTableView:tableView];
    cell.lookCellModel = self.dataSource[indexPath.row];
    cell.Block_CellOpen = ^(GLGWButton * _Nonnull l_but) {
        if (l_but.tag == 125) {
            NSIndexPath *indexPath1=[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
            NSArray *indexArray=[NSArray arrayWithObject:indexPath1];
            [tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationNone];
        }else{
            DLog(@"联系人");
        }
    };
    return cell;
}

#pragma mark ----- GoodsTopDelegate
-(void)selectTopViewButTag:(NSInteger)tag_num{
    if (screenView || screeningView) {
        [screeningView dissmiss];
        [screenView dissmiss];
    }
    switch (tag_num) {
        case 180:{
            screeningView = [[AdressAndSortingView alloc] initWithFrame:CGRectMake(0, kY_With_View(topSelectView, 1), KScreenWidth, KScreenHeight - kHeightProportion_In_iPhoneTriploid(35)) theTableListType:ListType_AllAdress inSuperView:selfView];
        }
            break;
        case 181:{
            screeningView = [[AdressAndSortingView alloc] initWithFrame:CGRectMake(0, kY_With_View(topSelectView, 1), KScreenWidth, KScreenHeight - kHeightProportion_In_iPhoneTriploid(35)) theTableListType:ListType_Comprehensive inSuperView:selfView];
        }
            break;
        case 182:{
            CGFloat sc_height;
            if (iPhoneX) {
                sc_height = KScreenHeight - kHeightProportion_In_iPhoneTriploid(78) - 88 - kHeightProportion_In_iPhoneTriploid(34);
            }else{
                sc_height = KScreenHeight - kHeightProportion_In_iPhoneTriploid(78) - 64;
            }
            screenView = [[ScreeningView alloc] initWithFrame:CGRectMake(0, kY_With_View(topSelectView, 1), KScreenWidth, sc_height) inSuperView:selfView];
            
        }
            break;
            
        default:
            break;
    }
}

@end
